package com.xyyl.patient.service.mall;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.support.Pager;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.entity.mall.MallOrderInfo;
import com.xyyl.patient.entity.mall.MallUserAddress;
import com.xyyl.patient.entity.question.Prescription;
import com.xyyl.patient.entity.user.Activity;
import com.xyyl.patient.mapper.mall.MallOrderInfoMapper;
import com.xyyl.patient.mapper.mall.MallUserAddressMapper;
import com.xyyl.patient.mapper.question.PrescriptionMapper;
import com.xyyl.patient.mapper.user.ActivityMapper;

/**
 * @author @author kellie
 * @projectName:xyyl-oms-server
 * @ClassName: MallOrderInfo
 * @Description:MallOrderInfo
 * @date 2018-03-02 09:47:52
 */
@Service("mallOrderInfoService")
public class MallOrderInfoServiceImpl implements MallOrderInfoService {

    @Autowired
    private MallOrderInfoMapper mallOrderInfoMapper;

    @Autowired
    private MallUserAddressMapper mallUserAddressMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Override
    @Transactional
    public List<MallOrderInfo> queryList(Pager page) {
        List<MallOrderInfo> mallOrderInfoList = mallOrderInfoMapper.queryForPage(page);


        return mallOrderInfoList;
    }

    @Override
    @Transactional
    public MallOrderInfo queryObject(Integer id) {
        return mallOrderInfoMapper.selectOne(id);
    }

    @Override
    @Transactional
    public int saveMallOrderInfo(MallOrderInfo MallOrderInfo) {
        //获取处方单
        Prescription prescription = prescriptionMapper.queryPrescriptionOne(MallOrderInfo.getPrescriptionId());
        MallUserAddress mallUserAddress = new MallUserAddress();
        //获取收货地址
        if (prescription != null && prescription.getPatientuserId() != null && prescription.getIsbuy() != 1) {
            //是否获取默认地址
            if (MallOrderInfo.getUserId() != null) {
                mallUserAddress = mallUserAddressMapper.selectOne(MallOrderInfo.getUserId());
            } else {
                mallUserAddress.setIsDefault(1);
                mallUserAddress.setUserId(prescription.getPatientuserId());
                mallUserAddress = mallUserAddressMapper.selectList(mallUserAddress).get(0);
            }

        }
		Activity activity = activityMapper.selectOne(3);
        BigDecimal a = prescription.getTotlmoney();
        //应付款金额
        if (activity != null) {
            a = prescription.getTotlmoney().multiply(activity.getPrice());
        }
        MallOrderInfo.setPrescription(prescription.getName());
        MallOrderInfo.setAddress(mallUserAddress.getAddress());
        MallOrderInfo.setConsignee(mallUserAddress.getConsignee());
        MallOrderInfo.setGoodsAmount(prescription.getTotlmoney());
        MallOrderInfo.setOrderAmount(a);

        MallOrderInfo.setMobile(mallUserAddress.getMobile());
        MallOrderInfo.setPayStatus(AppEnum.common.Pay_status_not.getValue());// '支付状态;0未付款;1已付款',
        MallOrderInfo.setShippingStatus(AppEnum.common.Pay_status_shipping_not.getValue());// '商品配送情况;0未发货,1已发货,2已收货,4售后',
        MallOrderInfo.setOrderStatus(AppEnum.common.Order_status_confirm.getValue());// '订单的状态;0未确认,1确认,2已取消,3无效,4退货',
        MallOrderInfo.setPayId(AppEnum.common.Order_payType_wx.getValue());// '0余额,1微信支付',
        MallOrderInfo.setUserId(prescription.getPatientuserId());
        String preCode = DateUtils.formatDatetime(new Date(), "yyyyMMddHHss") + RandomUtils.nextInt(99999);
        MallOrderInfo.setOrdersn("GY-" + preCode);
        mallOrderInfoMapper.insert(MallOrderInfo);
        return MallOrderInfo.getId();
    }

    @Override
    @Transactional
    public int updateMallOrderInfo(MallOrderInfo MallOrderInfo) {
        return mallOrderInfoMapper.update(MallOrderInfo);
    }

    @Override
    @Transactional
    public int updatePrescription(Prescription prescription) {
        return prescriptionMapper.update(prescription);
    }

    @Override
    @Transactional
    public int deleteMallOrderInfo(Integer id) {
        return mallOrderInfoMapper.delete(id);
    }

    @Override
    @Transactional
    public int batchDeleteMallOrderInfo(Integer[] ids) {

        return 0;
    }
}
